In [ ]:
# Importar bibliotecas necesarias
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller, kpss, coint
from statsmodels.tsa.seasonal import STL
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.tsa.arima.model import ARIMA as ARIMA_model
from sklearn.metrics import mean_squared_error
import warnings
warnings.filterwarnings("ignore")
# Crear DataFrame con los datos proporcionados
data = {
'Fecha_AMZN': ['2025-03-07 11:48:00', '2025-03-07 11:45:00', '2025-03-07 11:42:00', '2025-03-07 11:39:00', '2025-03-07 11:36:00', '2025-03-07 11:33:00', '2025-03-07 11:30:00', '2025-03-07 11:27:00', '2025-03-07 11:24:00', '2025-03-07 11:21:00', '2025-03-07 11:18:00', '2025-03-07 11:15:00', '2025-03-07 11:12:00', '2025-03-07 11:09:00', '2025-03-07 11:06:00', '2025-03-07 11:03:00', '2025-03-07 11:00:00', '2025-03-07 10:57:00', '2025-03-07 10:54:00', '2025-03-07 10:51:00', '2025-03-07 10:48:00', '2025-03-07 10:45:00', '2025-03-07 10:42:00', '2025-03-07 10:39:00', '2025-03-07 10:36:00', '2025-03-07 10:33:00', '2025-03-07 10:30:00', '2025-03-07 10:27:00', '2025-03-07 10:24:00', '2025-03-07 10:21:00', '2025-03-07 10:18:00', '2025-03-07 10:15:00', '2025-03-07 10:12:00', '2025-03-07 10:09:00'],
'AMZN_Lst_Trd': [195.75, 195.72, 195.565, 195.09, 195.075, 194.63, 194.085, 193.66, 193.575, 193.37, 193.005, 192.86, 193.27, 192.79, 193.08, 193.345, 193.8, 193.68, 193.65, 192.82, 192.95, 193.535, 194.04, 193.71, 193.94, 194.446, 194.2, 194.57, 194.67, 195.415, 195.48, 195.88, 196.2, 196.43],
'Fecha_TSLA': ['2025-03-07 11:39:00', '2025-03-07 11:36:00', '2025-03-07 11:33:00', '2025-03-07 11:30:00', '2025-03-07 11:27:00', '2025-03-07 11:24:00', '2025-03-07 11:21:00', '2025-03-07 11:18:00', '2025-03-07 11:15:00', '2025-03-07 11:12:00', '2025-03-07 11:09:00', '2025-03-07 11:06:00', '2025-03-07 11:03:00', '2025-03-07 11:00:00', '2025-03-07 10:57:00', '2025-03-07 10:54:00', '2025-03-07 10:51:00', '2025-03-07 10:48:00', '2025-03-07 10:45:00', '2025-03-07 10:42:00', '2025-03-07 10:39:00', '2025-03-07 10:36:00', '2025-03-07 10:33:00', '2025-03-07 10:30:00', '2025-03-07 10:27:00', '2025-03-07 10:24:00', '2025-03-07 10:21:00', '2025-03-07 10:18:00', '2025-03-07 10:15:00', '2025-03-07 10:12:00', '2025-03-07 10:09:00', '2025-03-07 10:06:00', '2025-03-07 10:03:00', '2025-03-07 10:00:00'],
'TSLA_Lst_Trd': [257.53, 257.33, 256.915, 255.86, 255.58, 254.6809, 254.4, 253.9485, 253.2299, 253.95, 252.71, 253.26, 253.825, 253.665, 252.73, 252.7282, 250.995, 251.94, 253.62, 253.31, 253.27, 253.97, 255.275, 254.9, 254.615, 255.39, 256.975, 256.53, 257.6586, 258.925, 258.63, 259.12, 258.77, 258.035],
'Fecha_SBUX': ['2025-03-07 11:42:00', '2025-03-07 11:39:00', '2025-03-07 11:36:00', '2025-03-07 11:33:00', '2025-03-07 11:30:00', '2025-03-07 11:27:00', '2025-03-07 11:24:00', '2025-03-07 11:21:00', '2025-03-07 11:18:00', '2025-03-07 11:15:00', '2025-03-07 11:12:00', '2025-03-07 11:09:00', '2025-03-07 11:06:00', '2025-03-07 11:03:00', '2025-03-07 11:00:00', '2025-03-07 10:57:00', '2025-03-07 10:54:00', '2025-03-07 10:51:00', '2025-03-07 10:48:00', '2025-03-07 10:45:00', '2025-03-07 10:42:00', '2025-03-07 10:39:00', '2025-03-07 10:36:00', '2025-03-07 10:33:00', '2025-03-07 10:30:00', '2025-03-07 10:27:00', '2025-03-07 10:24:00', '2025-03-07 10:21:00', '2025-03-07 10:18:00', '2025-03-07 10:15:00', '2025-03-07 10:12:00', '2025-03-07 10:09:00', '2025-03-07 10:06:00', '2025-03-07 10:03:00'],
'SBUX_Lst_Trd': [104.38, 104.19, 104.13, 103.92, 103.89, 103.92, 103.95, 103.91, 103.98, 103.96, 104.295, 104.01, 104.005, 103.94, 103.995, 104.08, 103.99, 103.75, 103.83, 103.87, 104.03, 104.01, 104.1638, 104.15, 104.06, 104.03, 103.855, 104.18, 104.14, 104.27, 104.43, 104.57, 104.51, 104.555],
'Fecha_NKE': ['2025-03-07 11:45:00', '2025-03-07 11:42:00', '2025-03-07 11:39:00', '2025-03-07 11:36:00', '2025-03-07 11:33:00', '2025-03-07 11:30:00', '2025-03-07 11:27:00', '2025-03-07 11:24:00', '2025-03-07 11:21:00', '2025-03-07 11:18:00', '2025-03-07 11:15:00', '2025-03-07 11:12:00', '2025-03-07 11:09:00', '2025-03-07 11:06:00', '2025-03-07 11:03:00', '2025-03-07 11:00:00', '2025-03-07 10:57:00', '2025-03-07 10:54:00', '2025-03-07 10:51:00', '2025-03-07 10:48:00', '2025-03-07 10:45:00', '2025-03-07 10:42:00', '2025-03-07 10:39:00', '2025-03-07 10:36:00', '2025-03-07 10:33:00', '2025-03-07 10:30:00', '2025-03-07 10:27:00', '2025-03-07 10:24:00', '2025-03-07 10:21:00', '2025-03-07 10:18:00', '2025-03-07 10:15:00', '2025-03-07 10:12:00', '2025-03-07 10:09:00', '2025-03-07 10:06:00'],
'NKE_Lst_Trd': [77.835, 77.75, 77.65, 77.64, 77.49, 77.48, 77.475, 77.49, 77.47, 77.37, 77.4, 77.45, 77.34, 77.3316, 77.37, 77.44, 77.345, 77.23, 77.04, 77.085, 77.14, 77.04, 77.25, 77.43, 77.415, 77.465, 77.435, 77.3, 77.42, 77.54, 77.56, 77.585, 77.565, 77.58]
}
# Convertir a DataFrame
df = pd.DataFrame(data)
# Convertir columnas de fechas a datetime
df['Fecha_AMZN'] = pd.to_datetime(df['Fecha_AMZN'])
df['Fecha_TSLA'] = pd.to_datetime(df['Fecha_TSLA'])
df['Fecha_SBUX'] = pd.to_datetime(df['Fecha_SBUX'])
df['Fecha_NKE'] = pd.to_datetime(df['Fecha_NKE'])
# Establecer las fechas como índices
amzn_series = pd.Series(df['AMZN_Lst_Trd'].values, index=df['Fecha_AMZN'], name='AMZN')
tsla_series = pd.Series(df['TSLA_Lst_Trd'].values, index=df['Fecha_TSLA'], name='TSLA')
sbux_series = pd.Series(df['SBUX_Lst_Trd'].values, index=df['Fecha_SBUX'], name='SBUX')
nke_series = pd.Series(df['NKE_Lst_Trd'].values, index=df['Fecha_NKE'], name='NKE')
# Diccionario para manejar todas las series
series_dict = {
'AMZN': amzn_series,
'TSLA': tsla_series,
'SBUX': sbux_series,
'NKE': nke_series
}
# 1. Graficar las series originales para análisis de tendencia
plt.figure(figsize=(12, 6))
for name, series in series_dict.items():
plt.plot(series, label=name)
plt.title('Closing Prices (Trend Analysis)')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.show()
# 2. Descomposición STL para evaluar tendencia y estacionalidad
for name, series in series_dict.items():
stl = STL(series, period=13) # Periodo tentativo para datos intradía
result = stl.fit()
# Graficar la descomposición
plt.figure(figsize=(10, 8))
plt.subplot(411)
plt.plot(series, label='Original')
plt.legend(loc='upper left')
plt.subplot(412)
plt.plot(result.trend, label='Trend')
plt.legend(loc='upper left')
plt.subplot(413)
plt.plot(result.seasonal, label='Seasonal')
plt.legend(loc='upper left')
plt.subplot(414)
plt.plot(result.resid, label='Residual')
plt.legend(loc='upper left')
plt.suptitle(f'STL Decomposition for {name}')
plt.tight_layout(rect=[0, 0, 1, 0.95])
plt.show()
# Evaluar si hay estacionalidad significativa
seasonal_std = np.std(result.seasonal)
series_std = np.std(series)
if seasonal_std / series_std < 0.1:
print(f"{name}: No significant seasonality detected (seasonal std / series std = {seasonal_std / series_std:.4f})")
else:
print(f"{name}: Possible seasonality detected (seasonal std / series std = {seasonal_std / series_std:.4f})")
# 3. Pruebas de raíz unitaria (ADF y KPSS)
def adf_test(series, name):
result = adfuller(series, autolag='AIC')
print(f'\nADF Test for {name} (Original):')
print(f'ADF Statistic: {result[0]:.4f}')
print(f'p-value: {result[1]:.4f}')
if result[1] <= 0.05:
print(f'Reject null hypothesis - {name} (Original) is stationary')
else:
print(f'Fail to reject null - {name} (Original) may be non-stationary')
def kpss_test(series, name):
result = kpss(series, regression='c')
print(f'\nKPSS Test for {name} (Original):')
print(f'KPSS Statistic: {result[0]:.4f}')
print(f'p-value: {result[1]:.4f}')
if result[1] <= 0.05:
print(f'Reject null hypothesis - {name} (Original) is non-stationary')
else:
print(f'Fail to reject null - {name} (Original) may be stationary')
# Aplicar pruebas a cada serie
for name, series in series_dict.items():
adf_test(series, name)
kpss_test(series, name)
# 4. Prueba de cointegración entre pares de series
def cointegration_test(series1, series2, name1, name2):
score, p_value, _ = coint(series1, series2)
print(f'\nCointegration Test between {name1} and {name2}:')
print(f'p-value: {p_value:.4f}')
if p_value < 0.05:
print(f'Reject null hypothesis - {name1} and {name2} are cointegrated')
else:
print(f'Fail to reject null - {name1} and {name2} are not cointegrated')
# Realizar prueba de cointegración para todos los pares posibles
series_list = list(series_dict.items())
for i in range(len(series_list)):
for j in range(i + 1, len(series_list)):
name1, series1 = series_list[i]
name2, series2 = series_list[j]
# Asegurarnos de que las series tengan el mismo índice
aligned_series1, aligned_series2 = series1.align(series2, join='inner')
cointegration_test(aligned_series1, aligned_series2, name1, name2)
# 5. Diferenciar las series para hacerlas estacionarias (si es necesario)
d = 1 # Número de diferenciaciones
diff_series_dict = {}
for name, series in series_dict.items():
diff_series = series.diff(d).dropna()
diff_series_dict[name] = diff_series
# Graficar series diferenciadas
plt.figure(figsize=(10, 4))
plt.plot(diff_series)
plt.title(f'{name} Differenced Series (d={d})')
plt.xlabel('Date')
plt.ylabel('Differenced Price')
plt.show()
# Pruebas de raíz unitaria en las series diferenciadas
adf_test(diff_series, f'{name} (Differenced)')
kpss_test(diff_series, f'{name} (Differenced)')
# 6. Dividir los datos en train y test (90% train, 10% test)
train_test_dict = {}
for name, series in series_dict.items():
train_size = int(len(series) * 0.9)
train, test = series[:train_size], series[train_size:]
train_test_dict[name] = (train, test)
# 7. Ajustar modelos ARMA en datos diferenciados y ARIMA en datos originales
arima_results = {}
for name, (train, test) in train_test_dict.items():
# Diferenciar datos de entrenamiento
diff_train = train.diff(d).dropna()
# Ajustar modelo ARMA (p, q) en datos diferenciados (d=0 porque ya está diferenciado)
best_aic = float('inf')
best_order = None
best_model = None
for p in range(3):
for q in range(3):
try:
model = ARIMA(diff_train, order=(p, 0, q))
model_fit = model.fit()
if model_fit.aic < best_aic:
best_aic = model_fit.aic
best_order = (p, 0, q)
best_model = model_fit
except:
continue
print(f'\nBest ARMA Model for {name} (Differenced): Order={best_order}, AIC={best_aic:.2f}')
# Pronosticar con ARMA (en escala diferenciada)
forecast_diff = best_model.forecast(steps=len(test))
print(f'Forecast (Differenced) for {name} (first 5 periods): {forecast_diff[:5].values}')
# Revertir la diferenciación para obtener pronósticos en la escala original
last_value = train.iloc[-1]
forecast = np.cumsum(np.concatenate([[0], forecast_diff])) + last_value
forecast = forecast[1:] # Eliminar el primer valor (0)
# Evaluar el modelo ARMA
mse = mean_squared_error(test, forecast)
print(f'MSE for {name} (ARMA): {mse:.2f}')
# Ajustar modelo ARIMA en datos originales
best_aic_arima = float('inf')
best_order_arima = None
best_model_arima = None
for p in range(3):
for d in range(2):
for q in range(3):
try:
model = ARIMA(train, order=(p, d, q))
model_fit = model.fit()
if model_fit.aic < best_aic_arima:
best_aic_arima = model_fit.aic
best_order_arima = (p, d, q)
best_model_arima = model_fit
except:
continue
print(f'\nBest ARIMA Model for {name}: Order={best_order_arima}, AIC={best_aic_arima:.2f}')
# Pronosticar con ARIMA
forecast_arima = best_model_arima.forecast(steps=len(test))
print(f'Forecast (ARIMA) for {name} (first 5 periods): {forecast_arima[:5].values}')
# Evaluar el modelo ARIMA
mse_arima = mean_squared_error(test, forecast_arima)
print(f'MSE for {name} (ARIMA): {mse_arima:.2f}')
# Guardar resultados
arima_results[name] = (best_model_arima, forecast_arima)
# Graficar pronósticos
plt.figure(figsize=(12, 6))
plt.plot(train.index, train, label='Train')
plt.plot(test.index, test, label='Test', color='green')
plt.plot(test.index, forecast_arima, label='ARIMA Forecast', color='red')
plt.title(f'{name} Closing Prices with ARIMA Forecast')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.show()
AMZN: Possible seasonality detected (seasonal std / series std = 0.4020)
TSLA: Possible seasonality detected (seasonal std / series std = 0.2688)
SBUX: Possible seasonality detected (seasonal std / series std = 0.6186)
NKE: Possible seasonality detected (seasonal std / series std = 0.4479) ADF Test for AMZN (Original): ADF Statistic: -0.4724 p-value: 0.8973 Fail to reject null - AMZN (Original) may be non-stationary KPSS Test for AMZN (Original): KPSS Statistic: 0.2668 p-value: 0.1000 Fail to reject null - AMZN (Original) may be stationary ADF Test for TSLA (Original): ADF Statistic: -0.9675 p-value: 0.7649 Fail to reject null - TSLA (Original) may be non-stationary KPSS Test for TSLA (Original): KPSS Statistic: 0.3539 p-value: 0.0970 Fail to reject null - TSLA (Original) may be stationary ADF Test for SBUX (Original): ADF Statistic: -1.6361 p-value: 0.4643 Fail to reject null - SBUX (Original) may be non-stationary KPSS Test for SBUX (Original): KPSS Statistic: 0.3770 p-value: 0.0871 Fail to reject null - SBUX (Original) may be stationary ADF Test for NKE (Original): ADF Statistic: -2.3597 p-value: 0.1534 Fail to reject null - NKE (Original) may be non-stationary KPSS Test for NKE (Original): KPSS Statistic: 0.2779 p-value: 0.1000 Fail to reject null - NKE (Original) may be stationary Cointegration Test between AMZN and TSLA: p-value: 0.7588 Fail to reject null - AMZN and TSLA are not cointegrated Cointegration Test between AMZN and SBUX: p-value: 0.6028 Fail to reject null - AMZN and SBUX are not cointegrated Cointegration Test between AMZN and NKE: p-value: 0.9488 Fail to reject null - AMZN and NKE are not cointegrated Cointegration Test between TSLA and SBUX: p-value: 0.4285 Fail to reject null - TSLA and SBUX are not cointegrated Cointegration Test between TSLA and NKE: p-value: 0.8528 Fail to reject null - TSLA and NKE are not cointegrated Cointegration Test between SBUX and NKE: p-value: 0.5015 Fail to reject null - SBUX and NKE are not cointegrated
ADF Test for AMZN (Differenced) (Original): ADF Statistic: -4.5585 p-value: 0.0002 Reject null hypothesis - AMZN (Differenced) (Original) is stationary KPSS Test for AMZN (Differenced) (Original): KPSS Statistic: 0.7276 p-value: 0.0110 Reject null hypothesis - AMZN (Differenced) (Original) is non-stationary
ADF Test for TSLA (Differenced) (Original): ADF Statistic: -5.2308 p-value: 0.0000 Reject null hypothesis - TSLA (Differenced) (Original) is stationary KPSS Test for TSLA (Differenced) (Original): KPSS Statistic: 0.5176 p-value: 0.0377 Reject null hypothesis - TSLA (Differenced) (Original) is non-stationary
ADF Test for SBUX (Differenced) (Original): ADF Statistic: -6.7711 p-value: 0.0000 Reject null hypothesis - SBUX (Differenced) (Original) is stationary KPSS Test for SBUX (Differenced) (Original): KPSS Statistic: 0.3557 p-value: 0.0963 Fail to reject null - SBUX (Differenced) (Original) may be stationary
ADF Test for NKE (Differenced) (Original): ADF Statistic: -4.7783 p-value: 0.0001 Reject null hypothesis - NKE (Differenced) (Original) is stationary KPSS Test for NKE (Differenced) (Original): KPSS Statistic: 0.3675 p-value: 0.0911 Fail to reject null - NKE (Differenced) (Original) may be stationary Best ARMA Model for AMZN (Differenced): Order=(0, 0, 0), AIC=31.51 Forecast (Differenced) for AMZN (first 5 periods): [-0.01155664 -0.01155664 -0.01155664 -0.01155664] MSE for AMZN (ARMA): 0.51 Best ARIMA Model for AMZN: Order=(0, 1, 0), AIC=29.54 Forecast (ARIMA) for AMZN (first 5 periods): [195.415 195.415 195.415 195.415] MSE for AMZN (ARIMA): 0.47
Best ARMA Model for TSLA (Differenced): Order=(0, 0, 0), AIC=78.21 Forecast (Differenced) for TSLA (first 5 periods): [0.04809849 0.04809849 0.04809849 0.04809849] MSE for TSLA (ARMA): 0.35 Best ARIMA Model for TSLA: Order=(0, 1, 0), AIC=76.30 Forecast (ARIMA) for TSLA (first 5 periods): [258.925 258.925 258.925 258.925] MSE for TSLA (ARIMA): 0.24
Best ARMA Model for SBUX (Differenced): Order=(0, 0, 0), AIC=-26.74 Forecast (Differenced) for SBUX (first 5 periods): [-0.00379824 -0.00379824 -0.00379824 -0.00379824] MSE for SBUX (ARMA): 0.07 Best ARIMA Model for SBUX: Order=(0, 0, 2), AIC=-37.17 Forecast (ARIMA) for SBUX (first 5 periods): [104.11594952 104.08658419 104.04379154 104.04379154] MSE for SBUX (ARIMA): 0.20
Best ARMA Model for NKE (Differenced): Order=(0, 0, 0), AIC=-49.32 Forecast (Differenced) for NKE (first 5 periods): [-0.01017745 -0.01017745 -0.01017745 -0.01017745] MSE for NKE (ARMA): 0.00 Best ARIMA Model for NKE: Order=(0, 1, 0), AIC=-51.00 Forecast (ARIMA) for NKE (first 5 periods): [77.54 77.54 77.54 77.54] MSE for NKE (ARIMA): 0.00
In [ ]:
# Importar bibliotecas necesarias
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller, kpss, coint
from statsmodels.tsa.seasonal import STL
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error
import warnings
warnings.filterwarnings("ignore")
# Importar la biblioteca para subir archivos en Google Colab
from google.colab import files
# Subir el archivo
uploaded = files.upload()
# Listar los archivos para verificar el nombre
!ls
# Especificar el nombre del archivo (ajusta según el nombre que veas en !ls)
excel_file = 'descargas bloomberg (9).xlsx' # Ajustado al archivo más reciente
# Listar todas las hojas disponibles en el archivo
print("Hojas disponibles en el archivo:", pd.ExcelFile(excel_file).sheet_names)
# Nombres de las hojas y correspondencia con las acciones
sheets_3days = {
'APPLE': 'AAPL',
'NVIDIA': 'NVDA',
'MSFT': 'MSFT',
'F': 'F'
}
sheets_10days = {
'APPLE 10 DÍAS': 'AAPL',
'NVIDIA 10 DÍAS': 'NVDA',
'MSFT 10 DÍAS': 'MSFT',
'F 10 DÍAS': 'F'
}
# Leer y procesar cada hoja
df_3days_3min = None
df_10days_30min = None
# Procesar hojas de 3 días
for sheet_name, stock in sheets_3days.items():
print(f"Procesando hoja (3 días): {sheet_name}")
# Leer la hoja, usando la primera fila como encabezado
df = pd.read_excel(excel_file, sheet_name=sheet_name, header=0)
# Inspeccionar las primeras filas
print(f"Primeras 5 filas de {sheet_name}:")
print(df.head())
# Verificar las columnas disponibles
print(f"Columnas en {sheet_name}: {df.columns.tolist()}")
# Crear DataFrame con fechas y precios
try:
df_temp = pd.DataFrame({
'Date': pd.to_datetime(df['Fecha']),
stock: df['Lst Trd/Lst Px']
}).dropna()
except ValueError as e:
print(f"Error al parsear fechas en {sheet_name}: {e}")
continue
print(f"Datos de 3 días para {stock}:")
print(df_temp.head())
# Combinar los datos en un solo DataFrame con inner join
if df_3days_3min is None:
df_3days_3min = df_temp.set_index('Date')
else:
df_3days_3min = df_3days_3min.join(df_temp.set_index('Date'), how='inner')
# Procesar hojas de 10 días
for sheet_name, stock in sheets_10days.items():
print(f"Procesando hoja (10 días): {sheet_name}")
# Leer la hoja, usando la primera fila como encabezado
df = pd.read_excel(excel_file, sheet_name=sheet_name, header=0)
# Inspeccionar las primeras filas
print(f"Primeras 5 filas de {sheet_name}:")
print(df.head())
# Verificar las columnas disponibles
print(f"Columnas en {sheet_name}: {df.columns.tolist()}")
# Crear DataFrame con fechas y precios
try:
df_temp = pd.DataFrame({
'Date': pd.to_datetime(df['Fecha']),
stock: df['Lst Trd/Lst Px']
}).dropna()
except ValueError as e:
print(f"Error al parsear fechas en {sheet_name}: {e}")
continue
print(f"Datos de 10 días para {stock}:")
print(df_temp.head())
# Combinar los datos en un solo DataFrame con inner join
if df_10days_30min is None:
df_10days_30min = df_temp.set_index('Date')
else:
df_10days_30min = df_10days_30min.join(df_temp.set_index('Date'), how='inner')
# Restablecer el índice para usar 'Date' como columna nuevamente
if df_3days_3min is not None and df_10days_30min is not None:
df_3days_3min = df_3days_3min.reset_index()
df_10days_30min = df_10days_30min.reset_index()
else:
print("Error: df_3days_3min o df_10days_30min son None. Revisa la estructura de los datos.")
raise ValueError("No se pudieron procesar los datos.")
# Lista de acciones a analizar
stocks = ['AAPL', 'NVDA', 'MSFT', 'F']
# Crear diccionarios de series temporales para ambos rangos
series_dict_3days_3min = {}
for stock in stocks:
series_dict_3days_3min[stock] = pd.Series(df_3days_3min[stock].values, index=df_3days_3min['Date'], name=stock)
series_dict_10days_30min = {}
for stock in stocks:
series_dict_10days_30min[stock] = pd.Series(df_10days_30min[stock].values, index=df_10days_30min['Date'], name=stock)
# Función para realizar el análisis (STL, ADF/KPSS, Cointegración, ARIMA)
def analyze_time_series(series_dict, title_prefix):
# 1. Graficar las series originales
plt.figure(figsize=(14, 7))
for name, series in series_dict.items():
plt.plot(series, label=name)
plt.title(f'{title_prefix} - Intraday Closing Prices')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
plt.show()
# 2. Descomposición STL
for name, series in series_dict.items():
# Verificar y limpiar NaN antes de STL
series_clean = series.dropna()
if len(series_clean) == 0:
print(f"Error: No hay datos válidos para {name} ({title_prefix}) después de eliminar NaN")
continue
# Ajustar el periodo según la frecuencia (aproximado)
period = 480 if "3 Days" in title_prefix else 48 # 480 para 3 días cada 3 minutos, 48 para 30 minutos
try:
stl = STL(series_clean, period=period)
result = stl.fit()
plt.figure(figsize=(10, 8))
plt.subplot(411)
plt.plot(series_clean, label='Original')
plt.legend()
plt.subplot(412)
plt.plot(result.trend, label='Trend')
plt.legend()
plt.subplot(413)
plt.plot(result.seasonal, label='Seasonal')
plt.legend()
plt.subplot(414)
plt.plot(result.resid, label='Residual')
plt.legend()
plt.suptitle(f'STL Decomposition for {name} ({title_prefix})')
plt.tight_layout()
plt.show()
seasonal_std = np.std(result.seasonal)
series_std = np.std(series_clean)
print(f"{name} ({title_prefix}): Seasonal std / Series std = {seasonal_std / series_std:.4f}")
except Exception as e:
print(f"Error in STL decomposition for {name} ({title_prefix}): {e}")
# 3. Pruebas de raíz unitaria (ADF y KPSS)
def adf_test(series, name):
result = adfuller(series.dropna(), autolag='AIC')
print(f'\nADF Test for {name} ({title_prefix}): p-value = {result[1]:.4f}')
return result[1] <= 0.05
def kpss_test(series, name):
result = kpss(series.dropna(), regression='c')
print(f'KPSS Test for {name} ({title_prefix}): p-value = {result[1]:.4f}')
return result[1] <= 0.05
for name, series in series_dict.items():
is_stationary_adf = adf_test(series, name)
is_stationary_kpss = kpss_test(series, name)
print(f"{name} ({title_prefix}) Stationarity: ADF={is_stationary_adf}, KPSS={is_stationary_kpss}")
# 4. Prueba de cointegración
def cointegration_test(series1, series2, name1, name2):
series1_clean = series1.dropna()
series2_clean = series2.dropna()
aligned_series1, aligned_series2 = series1_clean.align(series2_clean, join='inner')
if len(aligned_series1) == 0 or len(aligned_series2) == 0:
print(f"Error: No hay datos alineados para cointegración entre {name1} y {name2}")
return False
score, p_value, _ = coint(aligned_series1, aligned_series2)
print(f'\nCointegration Test {name1} vs {name2} ({title_prefix}): p-value = {p_value:.4f}')
return p_value < 0.05
series_list = list(series_dict.items())
for i in range(len(series_list)):
for j in range(i + 1, len(series_list)):
name1, series1 = series_list[i]
name2, series2 = series_list[j]
cointegration_test(series1, series2, name1, name2)
# 5. Modelos ARIMA
for name, series in series_dict.items():
series_clean = series.dropna()
if len(series_clean) == 0:
print(f"Error: No hay datos válidos para ARIMA en {name} ({title_prefix})")
continue
train_size = int(len(series_clean) * 0.9)
train, test = series_clean[:train_size], series_clean[train_size:]
# Ajustar ARIMA
best_aic = float('inf')
best_order = None
best_model = None
for p in range(3):
for d in range(2):
for q in range(3):
try:
model = ARIMA(train, order=(p, d, q))
model_fit = model.fit()
if model_fit.aic < best_aic:
best_aic = model_fit.aic
best_order = (p, d, q)
best_model = model_fit
except:
continue
print(f'\nBest ARIMA for {name} ({title_prefix}): Order={best_order}, AIC={best_aic:.2f}')
forecast = best_model.forecast(steps=len(test))
mse = mean_squared_error(test, forecast)
print(f'MSE for {name} ({title_prefix}): {mse:.2f}')
plt.figure(figsize=(10, 4))
plt.plot(train.index, train, label='Train')
plt.plot(test.index, test, label='Test')
plt.plot(test.index, forecast, label='Forecast')
plt.title(f'{name} with ARIMA Forecast ({title_prefix})')
plt.legend()
plt.show()
# Analizar ambos rangos de datos
if df_3days_3min is not None and df_10days_30min is not None:
print("=== Analysis for Last 3 Days (Every 3 Minutes) ===")
analyze_time_series(series_dict_3days_3min, "Last 3 Days (3 min)")
print("\n=== Analysis for Last 10 Days (Every 30 Minutes) ===")
analyze_time_series(series_dict_10days_30min, "Last 10 Days (30 min)")
else:
print("Error: df_3days_3min o df_10days_30min son None. Revisa la estructura de los datos.")
raise ValueError("No se pudieron procesar los datos.")
Saving descargas bloomberg.xlsx to descargas bloomberg (10).xlsx
'descargas bloomberg (10).xlsx' 'descargas bloomberg (4).xlsx' 'descargas bloomberg (8).xlsx'
'descargas bloomberg (1).xlsx' 'descargas bloomberg (5).xlsx' 'descargas bloomberg (9).xlsx'
'descargas bloomberg (2).xlsx' 'descargas bloomberg (6).xlsx' 'descargas bloomberg.xlsx'
'descargas bloomberg (3).xlsx' 'descargas bloomberg (7).xlsx' sample_data
Hojas disponibles en el archivo: ['AMAZON', 'APPLE', 'APPLE 10 DÍAS', 'NVIDIA', 'NVIDIA 10 DÍAS', 'MSFT', 'MSFT 10 DÍAS', 'F', 'F 10 DÍAS', 'TSLA', 'SBUX', 'NKE']
Procesando hoja (3 días): APPLE
Primeras 5 filas de APPLE:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:21:00 239.03 110.946k 241.44k
1 2025-03-07 11:18:00 238.89 183.729k 250.773k
2 2025-03-07 11:15:00 238.62 115.774k 257.897k
3 2025-03-07 11:12:00 238.70 167.875k 269.943k
4 2025-03-07 11:09:00 238.11 245.826k 275.014k
Columnas en APPLE: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 3 días para AAPL:
Date AAPL
0 2025-03-07 11:21:00 239.03
1 2025-03-07 11:18:00 238.89
2 2025-03-07 11:15:00 238.62
3 2025-03-07 11:12:00 238.70
4 2025-03-07 11:09:00 238.11
Procesando hoja (3 días): NVIDIA
Primeras 5 filas de NVIDIA:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:24:00 108.8100 884.24k 2.067M
1 2025-03-07 11:21:00 108.6499 1.686M 2.21M
2 2025-03-07 11:18:00 108.2400 1.361M 2.353M
3 2025-03-07 11:15:00 107.9900 1.241M 2.362M
4 2025-03-07 11:12:00 108.2450 1.66M 2.377M
Columnas en NVIDIA: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 3 días para NVDA:
Date NVDA
0 2025-03-07 11:24:00 108.8100
1 2025-03-07 11:21:00 108.6499
2 2025-03-07 11:18:00 108.2400
3 2025-03-07 11:15:00 107.9900
4 2025-03-07 11:12:00 108.2450
Procesando hoja (3 días): MSFT
Primeras 5 filas de MSFT:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:30:00 387.750 8352 66633
1 2025-03-07 11:27:00 387.340 56145 72417
2 2025-03-07 11:24:00 387.200 45427 75017
3 2025-03-07 11:21:00 386.635 81545 79070
4 2025-03-07 11:18:00 386.050 47115 79166
Columnas en MSFT: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 3 días para MSFT:
Date MSFT
0 2025-03-07 11:30:00 387.750
1 2025-03-07 11:27:00 387.340
2 2025-03-07 11:24:00 387.200
3 2025-03-07 11:21:00 386.635
4 2025-03-07 11:18:00 386.050
Procesando hoja (3 días): F
Primeras 5 filas de F:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:36:00 9.8001 118.124k 779.909k
1 2025-03-07 11:33:00 9.7999 667.432k 807.469k
2 2025-03-07 11:30:00 9.7800 601.267k 801.849k
3 2025-03-07 11:27:00 9.7600 688.325k 841.22k
4 2025-03-07 11:24:00 9.7350 448.437k 863.239k
Columnas en F: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 3 días para F:
Date F
0 2025-03-07 11:36:00 9.8001
1 2025-03-07 11:33:00 9.7999
2 2025-03-07 11:30:00 9.7800
3 2025-03-07 11:27:00 9.7600
4 2025-03-07 11:24:00 9.7350
Procesando hoja (10 días): APPLE 10 DÍAS
Primeras 5 filas de APPLE 10 DÍAS:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:30:00 239.385 413.564k 2.586M
1 2025-03-07 11:00:00 239.160 2.066M 2.658M
2 2025-03-07 10:30:00 239.235 2.825M 2.621M
3 2025-03-07 10:00:00 239.095 2.202M 2.548M
4 2025-03-07 09:30:00 238.930 2.842M 2.558M
Columnas en APPLE 10 DÍAS: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 10 días para AAPL:
Date AAPL
0 2025-03-07 11:30:00 239.385
1 2025-03-07 11:00:00 239.160
2 2025-03-07 10:30:00 239.235
3 2025-03-07 10:00:00 239.095
4 2025-03-07 09:30:00 238.930
Procesando hoja (10 días): NVIDIA 10 DÍAS
Primeras 5 filas de NVIDIA 10 DÍAS:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:30:00 109.5850 2.224M 19.998M
1 2025-03-07 11:00:00 108.8400 16.316M 20.847M
2 2025-03-07 10:30:00 107.9299 26.742M 20.593M
3 2025-03-07 10:00:00 109.7600 15.596M 19.69M
4 2025-03-07 09:30:00 111.1500 29.536M 19.99M
Columnas en NVIDIA 10 DÍAS: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 10 días para NVDA:
Date NVDA
0 2025-03-07 11:30:00 109.5850
1 2025-03-07 11:00:00 108.8400
2 2025-03-07 10:30:00 107.9299
3 2025-03-07 10:00:00 109.7600
4 2025-03-07 09:30:00 111.1500
Procesando hoja (10 días): MSFT 10 DÍAS
Primeras 5 filas de MSFT 10 DÍAS:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:30:00 387.3100 32107 1.041M
1 2025-03-07 11:00:00 387.3400 661.803k 1.078M
2 2025-03-07 10:30:00 386.7075 882.52k 1.082M
3 2025-03-07 10:00:00 388.1000 645.911k 1.071M
4 2025-03-07 09:30:00 390.7500 1.044M 1.108M
Columnas en MSFT 10 DÍAS: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 10 días para MSFT:
Date MSFT
0 2025-03-07 11:30:00 387.3100
1 2025-03-07 11:00:00 387.3400
2 2025-03-07 10:30:00 386.7075
3 2025-03-07 10:00:00 388.1000
4 2025-03-07 09:30:00 390.7500
Procesando hoja (10 días): F 10 DÍAS
Primeras 5 filas de F 10 DÍAS:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:30:00 9.8100 1.609M 9.993M
1 2025-03-07 11:00:00 9.7600 8.258M 10.761M
2 2025-03-07 10:30:00 9.7150 11.066M 10.766M
3 2025-03-07 10:00:00 9.7943 9.683M 10.721M
4 2025-03-07 09:30:00 9.8350 21.069M 10.89M
Columnas en F 10 DÍAS: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 10 días para F:
Date F
0 2025-03-07 11:30:00 9.8100
1 2025-03-07 11:00:00 9.7600
2 2025-03-07 10:30:00 9.7150
3 2025-03-07 10:00:00 9.7943
4 2025-03-07 09:30:00 9.8350
=== Analysis for Last 3 Days (Every 3 Minutes) ===
AAPL (Last 3 Days (3 min)): Seasonal std / Series std = 1.0000
NVDA (Last 3 Days (3 min)): Seasonal std / Series std = 1.0000
MSFT (Last 3 Days (3 min)): Seasonal std / Series std = 1.0000
F (Last 3 Days (3 min)): Seasonal std / Series std = 1.0000 ADF Test for AAPL (Last 3 Days (3 min)): p-value = 0.4023 KPSS Test for AAPL (Last 3 Days (3 min)): p-value = 0.0593 AAPL (Last 3 Days (3 min)) Stationarity: ADF=False, KPSS=False ADF Test for NVDA (Last 3 Days (3 min)): p-value = 0.2327 KPSS Test for NVDA (Last 3 Days (3 min)): p-value = 0.0100 NVDA (Last 3 Days (3 min)) Stationarity: ADF=False, KPSS=True ADF Test for MSFT (Last 3 Days (3 min)): p-value = 0.8522 KPSS Test for MSFT (Last 3 Days (3 min)): p-value = 0.0100 MSFT (Last 3 Days (3 min)) Stationarity: ADF=False, KPSS=True ADF Test for F (Last 3 Days (3 min)): p-value = 0.8124 KPSS Test for F (Last 3 Days (3 min)): p-value = 0.0100 F (Last 3 Days (3 min)) Stationarity: ADF=False, KPSS=True Cointegration Test AAPL vs NVDA (Last 3 Days (3 min)): p-value = 0.5942 Cointegration Test AAPL vs MSFT (Last 3 Days (3 min)): p-value = 0.3644 Cointegration Test AAPL vs F (Last 3 Days (3 min)): p-value = 0.5458 Cointegration Test NVDA vs MSFT (Last 3 Days (3 min)): p-value = 0.5100 Cointegration Test NVDA vs F (Last 3 Days (3 min)): p-value = 0.5173 Cointegration Test MSFT vs F (Last 3 Days (3 min)): p-value = 0.2781 Best ARIMA for AAPL (Last 3 Days (3 min)): Order=(0, 1, 0), AIC=367.55 MSE for AAPL (Last 3 Days (3 min)): 0.20
Best ARIMA for NVDA (Last 3 Days (3 min)): Order=(2, 1, 0), AIC=445.27 MSE for NVDA (Last 3 Days (3 min)): 12.09
Best ARIMA for MSFT (Last 3 Days (3 min)): Order=(2, 1, 2), AIC=750.94 MSE for MSFT (Last 3 Days (3 min)): 1.40
Best ARIMA for F (Last 3 Days (3 min)): Order=(0, 1, 0), AIC=-1830.40 MSE for F (Last 3 Days (3 min)): 0.00
=== Analysis for Last 10 Days (Every 30 Minutes) ===
AAPL (Last 10 Days (30 min)): Seasonal std / Series std = 0.4350
NVDA (Last 10 Days (30 min)): Seasonal std / Series std = 0.2562
MSFT (Last 10 Days (30 min)): Seasonal std / Series std = 0.5260
F (Last 10 Days (30 min)): Seasonal std / Series std = 0.9377 ADF Test for AAPL (Last 10 Days (30 min)): p-value = 0.6997 KPSS Test for AAPL (Last 10 Days (30 min)): p-value = 0.0100 AAPL (Last 10 Days (30 min)) Stationarity: ADF=False, KPSS=True ADF Test for NVDA (Last 10 Days (30 min)): p-value = 0.8898 KPSS Test for NVDA (Last 10 Days (30 min)): p-value = 0.0100 NVDA (Last 10 Days (30 min)) Stationarity: ADF=False, KPSS=True ADF Test for MSFT (Last 10 Days (30 min)): p-value = 0.8289 KPSS Test for MSFT (Last 10 Days (30 min)): p-value = 0.0100 MSFT (Last 10 Days (30 min)) Stationarity: ADF=False, KPSS=True ADF Test for F (Last 10 Days (30 min)): p-value = 0.0498 KPSS Test for F (Last 10 Days (30 min)): p-value = 0.0836 F (Last 10 Days (30 min)) Stationarity: ADF=True, KPSS=False Cointegration Test AAPL vs NVDA (Last 10 Days (30 min)): p-value = 0.3201 Cointegration Test AAPL vs MSFT (Last 10 Days (30 min)): p-value = 0.5892 Cointegration Test AAPL vs F (Last 10 Days (30 min)): p-value = 0.7830 Cointegration Test NVDA vs MSFT (Last 10 Days (30 min)): p-value = 0.6511 Cointegration Test NVDA vs F (Last 10 Days (30 min)): p-value = 0.9000 Cointegration Test MSFT vs F (Last 10 Days (30 min)): p-value = 0.9290 Best ARIMA for AAPL (Last 10 Days (30 min)): Order=(0, 1, 0), AIC=371.16 MSE for AAPL (Last 10 Days (30 min)): 0.97
Best ARIMA for NVDA (Last 10 Days (30 min)): Order=(1, 1, 0), AIC=442.47 MSE for NVDA (Last 10 Days (30 min)): 25.88
Best ARIMA for MSFT (Last 10 Days (30 min)): Order=(0, 1, 0), AIC=457.43 MSE for MSFT (Last 10 Days (30 min)): 63.01
Best ARIMA for F (Last 10 Days (30 min)): Order=(2, 0, 1), AIC=-306.16 MSE for F (Last 10 Days (30 min)): 0.01
In [2]:
# Importar bibliotecas necesarias
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller, kpss, coint
from statsmodels.tsa.seasonal import STL
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error
import warnings
warnings.filterwarnings("ignore")
# Importar la biblioteca para subir archivos en Google Colab
from google.colab import files
# Subir el archivo
uploaded = files.upload()
# Listar los archivos para verificar el nombre
!ls
# Especificar el nombre del archivo (ajusta según el nombre que veas en !ls)
excel_file = 'descargas bloomberg.xlsx' # Ajustado al nombre del archivo subido
# Listar todas las hojas disponibles en el archivo
print("Hojas disponibles en el archivo:", pd.ExcelFile(excel_file).sheet_names)
# Nombres de las hojas y correspondencia con las acciones
sheets_3days = {
'APPLE': 'AAPL',
'NVIDIA': 'NVDA',
'MSFT': 'MSFT',
'F': 'F'
}
sheets_10days = {
'APPLE 10 DÍAS': 'AAPL',
'NVIDIA 10 DÍAS': 'NVDA',
'MSFT 10 DÍAS': 'MSFT',
'F 10 DÍAS': 'F'
}
# Leer y procesar cada hoja
df_3days_3min = None
df_10days_30min = None
# Procesar hojas de 3 días
for sheet_name, stock in sheets_3days.items():
print(f"Procesando hoja (3 días): {sheet_name}")
df = pd.read_excel(excel_file, sheet_name=sheet_name, header=0)
print(f"Primeras 5 filas de {sheet_name}:")
print(df.head())
print(f"Columnas en {sheet_name}: {df.columns.tolist()}")
try:
df_temp = pd.DataFrame({
'Date': pd.to_datetime(df['Fecha']),
stock: df['Lst Trd/Lst Px']
}).dropna()
except ValueError as e:
print(f"Error al parsear fechas en {sheet_name}: {e}")
continue
print(f"Datos de 3 días para {stock}:")
print(df_temp.head())
if df_3days_3min is None:
df_3days_3min = df_temp.set_index('Date')
else:
df_3days_3min = df_3days_3min.join(df_temp.set_index('Date'), how='inner')
# Procesar hojas de 10 días
for sheet_name, stock in sheets_10days.items():
print(f"Procesando hoja (10 días): {sheet_name}")
df = pd.read_excel(excel_file, sheet_name=sheet_name, header=0)
print(f"Primeras 5 filas de {sheet_name}:")
print(df.head())
print(f"Columnas en {sheet_name}: {df.columns.tolist()}")
try:
df_temp = pd.DataFrame({
'Date': pd.to_datetime(df['Fecha']),
stock: df['Lst Trd/Lst Px']
}).dropna()
except ValueError as e:
print(f"Error al parsear fechas en {sheet_name}: {e}")
continue
print(f"Datos de 10 días para {stock}:")
print(df_temp.head())
if df_10days_30min is None:
df_10days_30min = df_temp.set_index('Date')
else:
df_10days_30min = df_10days_30min.join(df_temp.set_index('Date'), how='inner')
# Restablecer el índice para usar 'Date' como columna nuevamente
if df_3days_3min is not None and df_10days_30min is not None:
df_3days_3min = df_3days_3min.reset_index()
df_10days_30min = df_10days_30min.reset_index()
else:
print("Error: df_3days_3min o df_10days_30min son None. Revisa la estructura de los datos.")
raise ValueError("No se pudieron procesar los datos.")
# Lista de acciones a analizar
stocks = ['AAPL', 'NVDA', 'MSFT', 'F']
# Crear diccionarios de series temporales para ambos rangos
series_dict_3days_3min = {}
for stock in stocks:
series_dict_3days_3min[stock] = pd.Series(df_3days_3min[stock].values, index=df_3days_3min['Date'], name=stock)
series_dict_10days_30min = {}
for stock in stocks:
series_dict_10days_30min[stock] = pd.Series(df_10days_30min[stock].values, index=df_10days_30min['Date'], name=stock)
# Estructura para almacenar resultados
results_3days = {'STL': {}, 'ADF': {}, 'KPSS': {}, 'Cointegration': [], 'ARIMA': {}}
results_10days = {'STL': {}, 'ADF': {}, 'KPSS': {}, 'Cointegration': [], 'ARIMA': {}}
# Función para realizar el análisis (STL, ADF/KPSS, Cointegración, ARIMA)
def analyze_time_series(series_dict, title_prefix, results_dict):
# 1. Graficar las series originales
plt.figure(figsize=(14, 7))
for name, series in series_dict.items():
plt.plot(series, label=name)
plt.title(f'{title_prefix} - Intraday Closing Prices')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
plt.show()
# 2. Descomposición STL
for name, series in series_dict.items():
series_clean = series.dropna()
if len(series_clean) == 0:
print(f"Error: No hay datos válidos para {name} ({title_prefix}) después de eliminar NaN")
continue
period = 48 if "3 Days" in title_prefix else 24 # Ajuste del período
try:
stl = STL(series_clean, period=period)
result = stl.fit()
plt.figure(figsize=(10, 8))
plt.subplot(411)
plt.plot(series_clean, label='Original')
plt.legend()
plt.subplot(412)
plt.plot(result.trend, label='Trend')
plt.legend()
plt.subplot(413)
plt.plot(result.seasonal, label='Seasonal')
plt.legend()
plt.subplot(414)
plt.plot(result.resid, label='Residual')
plt.legend()
plt.suptitle(f'STL Decomposition for {name} ({title_prefix})')
plt.tight_layout()
plt.show()
seasonal_std = np.std(result.seasonal)
series_std = np.std(series_clean)
ratio = seasonal_std / series_std
print(f"{name} ({title_prefix}): Seasonal std / Series std = {ratio:.4f}")
results_dict['STL'][name] = ratio
except Exception as e:
print(f"Error in STL decomposition for {name} ({title_prefix}): {e}")
# 3. Pruebas de raíz unitaria (ADF y KPSS)
def adf_test(series, name):
result = adfuller(series.dropna(), autolag='AIC')
p_value = result[1]
print(f'\nADF Test for {name} ({title_prefix}): p-value = {p_value:.4f}')
results_dict['ADF'][name] = p_value
return p_value <= 0.05
def kpss_test(series, name):
result = kpss(series.dropna(), regression='c')
p_value = result[1]
print(f'KPSS Test for {name} ({title_prefix}): p-value = {p_value:.4f}')
results_dict['KPSS'][name] = p_value
return p_value <= 0.05
for name, series in series_dict.items():
is_stationary_adf = adf_test(series, name)
is_stationary_kpss = kpss_test(series, name)
print(f"{name} ({title_prefix}) Stationarity: ADF={is_stationary_adf}, KPSS={is_stationary_kpss}")
# 4. Prueba de cointegración
def cointegration_test(series1, series2, name1, name2):
series1_clean = series1.dropna()
series2_clean = series2.dropna()
aligned_series1, aligned_series2 = series1_clean.align(series2_clean, join='inner')
if len(aligned_series1) == 0 or len(aligned_series2) == 0:
print(f"Error: No hay datos alineados para cointegración entre {name1} y {name2}")
return False
score, p_value, _ = coint(aligned_series1, aligned_series2)
print(f'\nCointegration Test {name1} vs {name2} ({title_prefix}): p-value = {p_value:.4f}')
results_dict['Cointegration'].append((name1, name2, p_value))
return p_value < 0.05
series_list = list(series_dict.items())
for i in range(len(series_list)):
for j in range(i + 1, len(series_list)):
name1, series1 = series_list[i]
name2, series2 = series_list[j]
cointegration_test(series1, series2, name1, name2)
# 5. Modelos ARIMA
for name, series in series_dict.items():
series_clean = series.dropna()
if len(series_clean) == 0:
print(f"Error: No hay datos válidos para ARIMA en {name} ({title_prefix})")
continue
train_size = int(len(series_clean) * 0.9)
train, test = series_clean[:train_size], series_clean[train_size:]
best_aic = float('inf')
best_order = None
best_model = None
for p in range(5): # Ampliar rango
for d in range(2):
for q in range(5):
try:
model = ARIMA(train, order=(p, d, q))
model_fit = model.fit()
if model_fit.aic < best_aic:
best_aic = model_fit.aic
best_order = (p, d, q)
best_model = model_fit
except:
continue
print(f'\nBest ARIMA for {name} ({title_prefix}): Order={best_order}, AIC={best_aic:.2f}')
forecast = best_model.forecast(steps=len(test))
mse = mean_squared_error(test, forecast)
print(f'MSE for {name} ({title_prefix}): {mse:.2f}')
plt.figure(figsize=(10, 4))
plt.plot(train.index, train, label='Train')
plt.plot(test.index, test, label='Test')
plt.plot(test.index, forecast, label='Forecast')
plt.title(f'{name} with ARIMA Forecast ({title_prefix})')
plt.legend()
plt.show()
results_dict['ARIMA'][name] = (best_order, best_aic, mse)
# Analizar ambos rangos de datos
if df_3days_3min is not None and df_10days_30min is not None:
print("=== Analysis for Last 3 Days (Every 3 Minutes) ===")
analyze_time_series(series_dict_3days_3min, "Last 3 Days (3 min)", results_3days)
print("\n=== Analysis for Last 10 Days (Every 30 Minutes) ===")
analyze_time_series(series_dict_10days_30min, "Last 10 Days (30 min)", results_10days)
else:
print("Error: df_3days_3min o df_10days_30min son None. Revisa la estructura de los datos.")
raise ValueError("No se pudieron procesar los datos.")
# Resumen de resultados
def print_summary(results, title):
print(f"\n=== Resumen de Resultados para {title} ===")
print("\n**STL Decomposition (Seasonal std / Series std):**")
for stock, ratio in results['STL'].items():
print(f"{stock}: {ratio:.4f}")
print("\n**ADF Test (p-values):**")
for stock, p_value in results['ADF'].items():
print(f"{stock}: {p_value:.4f} {'(Estacionaria)' if p_value <= 0.05 else '(No Estacionaria)'}")
print("\n**KPSS Test (p-values):**")
for stock, p_value in results['KPSS'].items():
print(f"{stock}: {p_value:.4f} {'(Estacionaria)' if p_value > 0.05 else '(No Estacionaria)'}")
print("\n**Cointegration Tests (p-values):**")
for name1, name2, p_value in results['Cointegration']:
print(f"{name1} vs {name2}: {p_value:.4f} {'(Cointegradas)' if p_value < 0.05 else '(No Cointegradas)'}")
print("\n**ARIMA Models:**")
for stock, (order, aic, mse) in results['ARIMA'].items():
print(f"{stock}: Order={order}, AIC={aic:.2f}, MSE={mse:.2f}")
print_summary(results_3days, "Last 3 Days (Every 3 Minutes)")
print_summary(results_10days, "Last 10 Days (Every 30 Minutes)")
Saving descargas bloomberg.xlsx to descargas bloomberg (1).xlsx
'descargas bloomberg (1).xlsx' 'descargas bloomberg.xlsx' sample_data
Hojas disponibles en el archivo: ['AMAZON', 'APPLE', 'APPLE 10 DÍAS', 'NVIDIA', 'NVIDIA 10 DÍAS', 'MSFT', 'MSFT 10 DÍAS', 'F', 'F 10 DÍAS', 'TSLA', 'SBUX', 'NKE']
Procesando hoja (3 días): APPLE
Primeras 5 filas de APPLE:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:21:00 239.03 110.946k 241.44k
1 2025-03-07 11:18:00 238.89 183.729k 250.773k
2 2025-03-07 11:15:00 238.62 115.774k 257.897k
3 2025-03-07 11:12:00 238.70 167.875k 269.943k
4 2025-03-07 11:09:00 238.11 245.826k 275.014k
Columnas en APPLE: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 3 días para AAPL:
Date AAPL
0 2025-03-07 11:21:00 239.03
1 2025-03-07 11:18:00 238.89
2 2025-03-07 11:15:00 238.62
3 2025-03-07 11:12:00 238.70
4 2025-03-07 11:09:00 238.11
Procesando hoja (3 días): NVIDIA
Primeras 5 filas de NVIDIA:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:24:00 108.8100 884.24k 2.067M
1 2025-03-07 11:21:00 108.6499 1.686M 2.21M
2 2025-03-07 11:18:00 108.2400 1.361M 2.353M
3 2025-03-07 11:15:00 107.9900 1.241M 2.362M
4 2025-03-07 11:12:00 108.2450 1.66M 2.377M
Columnas en NVIDIA: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 3 días para NVDA:
Date NVDA
0 2025-03-07 11:24:00 108.8100
1 2025-03-07 11:21:00 108.6499
2 2025-03-07 11:18:00 108.2400
3 2025-03-07 11:15:00 107.9900
4 2025-03-07 11:12:00 108.2450
Procesando hoja (3 días): MSFT
Primeras 5 filas de MSFT:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:30:00 387.750 8352 66633
1 2025-03-07 11:27:00 387.340 56145 72417
2 2025-03-07 11:24:00 387.200 45427 75017
3 2025-03-07 11:21:00 386.635 81545 79070
4 2025-03-07 11:18:00 386.050 47115 79166
Columnas en MSFT: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 3 días para MSFT:
Date MSFT
0 2025-03-07 11:30:00 387.750
1 2025-03-07 11:27:00 387.340
2 2025-03-07 11:24:00 387.200
3 2025-03-07 11:21:00 386.635
4 2025-03-07 11:18:00 386.050
Procesando hoja (3 días): F
Primeras 5 filas de F:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:36:00 9.8001 118.124k 779.909k
1 2025-03-07 11:33:00 9.7999 667.432k 807.469k
2 2025-03-07 11:30:00 9.7800 601.267k 801.849k
3 2025-03-07 11:27:00 9.7600 688.325k 841.22k
4 2025-03-07 11:24:00 9.7350 448.437k 863.239k
Columnas en F: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 3 días para F:
Date F
0 2025-03-07 11:36:00 9.8001
1 2025-03-07 11:33:00 9.7999
2 2025-03-07 11:30:00 9.7800
3 2025-03-07 11:27:00 9.7600
4 2025-03-07 11:24:00 9.7350
Procesando hoja (10 días): APPLE 10 DÍAS
Primeras 5 filas de APPLE 10 DÍAS:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:30:00 239.385 413.564k 2.586M
1 2025-03-07 11:00:00 239.160 2.066M 2.658M
2 2025-03-07 10:30:00 239.235 2.825M 2.621M
3 2025-03-07 10:00:00 239.095 2.202M 2.548M
4 2025-03-07 09:30:00 238.930 2.842M 2.558M
Columnas en APPLE 10 DÍAS: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 10 días para AAPL:
Date AAPL
0 2025-03-07 11:30:00 239.385
1 2025-03-07 11:00:00 239.160
2 2025-03-07 10:30:00 239.235
3 2025-03-07 10:00:00 239.095
4 2025-03-07 09:30:00 238.930
Procesando hoja (10 días): NVIDIA 10 DÍAS
Primeras 5 filas de NVIDIA 10 DÍAS:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:30:00 109.5850 2.224M 19.998M
1 2025-03-07 11:00:00 108.8400 16.316M 20.847M
2 2025-03-07 10:30:00 107.9299 26.742M 20.593M
3 2025-03-07 10:00:00 109.7600 15.596M 19.69M
4 2025-03-07 09:30:00 111.1500 29.536M 19.99M
Columnas en NVIDIA 10 DÍAS: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 10 días para NVDA:
Date NVDA
0 2025-03-07 11:30:00 109.5850
1 2025-03-07 11:00:00 108.8400
2 2025-03-07 10:30:00 107.9299
3 2025-03-07 10:00:00 109.7600
4 2025-03-07 09:30:00 111.1500
Procesando hoja (10 días): MSFT 10 DÍAS
Primeras 5 filas de MSFT 10 DÍAS:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:30:00 387.3100 32107 1.041M
1 2025-03-07 11:00:00 387.3400 661.803k 1.078M
2 2025-03-07 10:30:00 386.7075 882.52k 1.082M
3 2025-03-07 10:00:00 388.1000 645.911k 1.071M
4 2025-03-07 09:30:00 390.7500 1.044M 1.108M
Columnas en MSFT 10 DÍAS: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 10 días para MSFT:
Date MSFT
0 2025-03-07 11:30:00 387.3100
1 2025-03-07 11:00:00 387.3400
2 2025-03-07 10:30:00 386.7075
3 2025-03-07 10:00:00 388.1000
4 2025-03-07 09:30:00 390.7500
Procesando hoja (10 días): F 10 DÍAS
Primeras 5 filas de F 10 DÍAS:
Fecha Lst Trd/Lst Px Volume SMAVG (15)
0 2025-03-07 11:30:00 9.8100 1.609M 9.993M
1 2025-03-07 11:00:00 9.7600 8.258M 10.761M
2 2025-03-07 10:30:00 9.7150 11.066M 10.766M
3 2025-03-07 10:00:00 9.7943 9.683M 10.721M
4 2025-03-07 09:30:00 9.8350 21.069M 10.89M
Columnas en F 10 DÍAS: ['Fecha', 'Lst Trd/Lst Px', 'Volume', 'SMAVG (15)']
Datos de 10 días para F:
Date F
0 2025-03-07 11:30:00 9.8100
1 2025-03-07 11:00:00 9.7600
2 2025-03-07 10:30:00 9.7150
3 2025-03-07 10:00:00 9.7943
4 2025-03-07 09:30:00 9.8350
=== Analysis for Last 3 Days (Every 3 Minutes) ===
AAPL (Last 3 Days (3 min)): Seasonal std / Series std = 0.1577
NVDA (Last 3 Days (3 min)): Seasonal std / Series std = 0.1703
MSFT (Last 3 Days (3 min)): Seasonal std / Series std = 0.1451
F (Last 3 Days (3 min)): Seasonal std / Series std = 0.1499 ADF Test for AAPL (Last 3 Days (3 min)): p-value = 0.4023 KPSS Test for AAPL (Last 3 Days (3 min)): p-value = 0.0593 AAPL (Last 3 Days (3 min)) Stationarity: ADF=False, KPSS=False ADF Test for NVDA (Last 3 Days (3 min)): p-value = 0.2327 KPSS Test for NVDA (Last 3 Days (3 min)): p-value = 0.0100 NVDA (Last 3 Days (3 min)) Stationarity: ADF=False, KPSS=True ADF Test for MSFT (Last 3 Days (3 min)): p-value = 0.8522 KPSS Test for MSFT (Last 3 Days (3 min)): p-value = 0.0100 MSFT (Last 3 Days (3 min)) Stationarity: ADF=False, KPSS=True ADF Test for F (Last 3 Days (3 min)): p-value = 0.8124 KPSS Test for F (Last 3 Days (3 min)): p-value = 0.0100 F (Last 3 Days (3 min)) Stationarity: ADF=False, KPSS=True Cointegration Test AAPL vs NVDA (Last 3 Days (3 min)): p-value = 0.5942 Cointegration Test AAPL vs MSFT (Last 3 Days (3 min)): p-value = 0.3644 Cointegration Test AAPL vs F (Last 3 Days (3 min)): p-value = 0.5458 Cointegration Test NVDA vs MSFT (Last 3 Days (3 min)): p-value = 0.5100 Cointegration Test NVDA vs F (Last 3 Days (3 min)): p-value = 0.5173 Cointegration Test MSFT vs F (Last 3 Days (3 min)): p-value = 0.2781 Best ARIMA for AAPL (Last 3 Days (3 min)): Order=(0, 1, 0), AIC=367.55 MSE for AAPL (Last 3 Days (3 min)): 0.20
Best ARIMA for NVDA (Last 3 Days (3 min)): Order=(2, 1, 0), AIC=445.27 MSE for NVDA (Last 3 Days (3 min)): 12.09
Best ARIMA for MSFT (Last 3 Days (3 min)): Order=(2, 1, 2), AIC=750.94 MSE for MSFT (Last 3 Days (3 min)): 1.40
Best ARIMA for F (Last 3 Days (3 min)): Order=(0, 1, 0), AIC=-1830.40 MSE for F (Last 3 Days (3 min)): 0.00
=== Analysis for Last 10 Days (Every 30 Minutes) ===
AAPL (Last 10 Days (30 min)): Seasonal std / Series std = 0.1933
NVDA (Last 10 Days (30 min)): Seasonal std / Series std = 0.1148
MSFT (Last 10 Days (30 min)): Seasonal std / Series std = 0.2096
F (Last 10 Days (30 min)): Seasonal std / Series std = 0.3838 ADF Test for AAPL (Last 10 Days (30 min)): p-value = 0.6997 KPSS Test for AAPL (Last 10 Days (30 min)): p-value = 0.0100 AAPL (Last 10 Days (30 min)) Stationarity: ADF=False, KPSS=True ADF Test for NVDA (Last 10 Days (30 min)): p-value = 0.8898 KPSS Test for NVDA (Last 10 Days (30 min)): p-value = 0.0100 NVDA (Last 10 Days (30 min)) Stationarity: ADF=False, KPSS=True ADF Test for MSFT (Last 10 Days (30 min)): p-value = 0.8289 KPSS Test for MSFT (Last 10 Days (30 min)): p-value = 0.0100 MSFT (Last 10 Days (30 min)) Stationarity: ADF=False, KPSS=True ADF Test for F (Last 10 Days (30 min)): p-value = 0.0498 KPSS Test for F (Last 10 Days (30 min)): p-value = 0.0836 F (Last 10 Days (30 min)) Stationarity: ADF=True, KPSS=False Cointegration Test AAPL vs NVDA (Last 10 Days (30 min)): p-value = 0.3201 Cointegration Test AAPL vs MSFT (Last 10 Days (30 min)): p-value = 0.5892 Cointegration Test AAPL vs F (Last 10 Days (30 min)): p-value = 0.7830 Cointegration Test NVDA vs MSFT (Last 10 Days (30 min)): p-value = 0.6511 Cointegration Test NVDA vs F (Last 10 Days (30 min)): p-value = 0.9000 Cointegration Test MSFT vs F (Last 10 Days (30 min)): p-value = 0.9290 Best ARIMA for AAPL (Last 10 Days (30 min)): Order=(0, 1, 0), AIC=371.16 MSE for AAPL (Last 10 Days (30 min)): 0.97
Best ARIMA for NVDA (Last 10 Days (30 min)): Order=(1, 1, 0), AIC=442.47 MSE for NVDA (Last 10 Days (30 min)): 25.88
Best ARIMA for MSFT (Last 10 Days (30 min)): Order=(0, 1, 0), AIC=457.43 MSE for MSFT (Last 10 Days (30 min)): 63.01
Best ARIMA for F (Last 10 Days (30 min)): Order=(3, 0, 2), AIC=-306.38 MSE for F (Last 10 Days (30 min)): 0.03
=== Resumen de Resultados para Last 3 Days (Every 3 Minutes) === **STL Decomposition (Seasonal std / Series std):** AAPL: 0.1577 NVDA: 0.1703 MSFT: 0.1451 F: 0.1499 **ADF Test (p-values):** AAPL: 0.4023 (No Estacionaria) NVDA: 0.2327 (No Estacionaria) MSFT: 0.8522 (No Estacionaria) F: 0.8124 (No Estacionaria) **KPSS Test (p-values):** AAPL: 0.0593 (Estacionaria) NVDA: 0.0100 (No Estacionaria) MSFT: 0.0100 (No Estacionaria) F: 0.0100 (No Estacionaria) **Cointegration Tests (p-values):** AAPL vs NVDA: 0.5942 (No Cointegradas) AAPL vs MSFT: 0.3644 (No Cointegradas) AAPL vs F: 0.5458 (No Cointegradas) NVDA vs MSFT: 0.5100 (No Cointegradas) NVDA vs F: 0.5173 (No Cointegradas) MSFT vs F: 0.2781 (No Cointegradas) **ARIMA Models:** AAPL: Order=(0, 1, 0), AIC=367.55, MSE=0.20 NVDA: Order=(2, 1, 0), AIC=445.27, MSE=12.09 MSFT: Order=(2, 1, 2), AIC=750.94, MSE=1.40 F: Order=(0, 1, 0), AIC=-1830.40, MSE=0.00 === Resumen de Resultados para Last 10 Days (Every 30 Minutes) === **STL Decomposition (Seasonal std / Series std):** AAPL: 0.1933 NVDA: 0.1148 MSFT: 0.2096 F: 0.3838 **ADF Test (p-values):** AAPL: 0.6997 (No Estacionaria) NVDA: 0.8898 (No Estacionaria) MSFT: 0.8289 (No Estacionaria) F: 0.0498 (Estacionaria) **KPSS Test (p-values):** AAPL: 0.0100 (No Estacionaria) NVDA: 0.0100 (No Estacionaria) MSFT: 0.0100 (No Estacionaria) F: 0.0836 (Estacionaria) **Cointegration Tests (p-values):** AAPL vs NVDA: 0.3201 (No Cointegradas) AAPL vs MSFT: 0.5892 (No Cointegradas) AAPL vs F: 0.7830 (No Cointegradas) NVDA vs MSFT: 0.6511 (No Cointegradas) NVDA vs F: 0.9000 (No Cointegradas) MSFT vs F: 0.9290 (No Cointegradas) **ARIMA Models:** AAPL: Order=(0, 1, 0), AIC=371.16, MSE=0.97 NVDA: Order=(1, 1, 0), AIC=442.47, MSE=25.88 MSFT: Order=(0, 1, 0), AIC=457.43, MSE=63.01 F: Order=(3, 0, 2), AIC=-306.38, MSE=0.03
In [9]:
from google.colab import drive
import os
# Montar Google Drive (si ya está montado, no hace nada)
drive.mount('/content/drive')
# Definir la carpeta raíz para buscar
root_folder = '/content/drive/My Drive/6to semestre: feb-jun 2025'
# Verificar que la carpeta raíz existe
if not os.path.exists(root_folder):
print(f"La carpeta {root_folder} no existe. Verifica el nombre.")
raise FileNotFoundError("Carpeta raíz no encontrada.")
# Función para buscar el archivo recursivamente
def find_file(start_path, file_name):
print(f"Buscando '{file_name}' en {start_path}...")
for root, dirs, files in os.walk(start_path):
for file in files:
if file_name.lower() in file.lower(): # Búsqueda insensible a mayúsculas
print(f"Archivo encontrado: {os.path.join(root, file)}")
for dir in dirs:
print(f"Subcarpeta encontrada: {os.path.join(root, dir)}")
# Buscar el archivo
find_file(root_folder, 'act clase bloomberg.ipynb')
# Listar el contenido de la carpeta principal para confirmar
print(f"\nContenido de {root_folder}:")
!ls "{root_folder}"
# Listar el contenido de la subcarpeta Series de tiempo (si existe)
subfolder_path = '/content/drive/My Drive/6to semestre: feb-jun 2025/series de tiempo'
if os.path.exists(subfolder_path):
print(f"\nContenido de {subfolder_path}:")
!ls "{subfolder_path}"
else:
print(f"La carpeta {subfolder_path} no existe. Verifica el nombre.")
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
Buscando 'act clase bloomberg.ipynb' en /content/drive/My Drive/6to semestre: feb-jun 2025...
Subcarpeta encontrada: /content/drive/My Drive/6to semestre: feb-jun 2025/diagnostico financiero
Subcarpeta encontrada: /content/drive/My Drive/6to semestre: feb-jun 2025/series de tiempo
Subcarpeta encontrada: /content/drive/My Drive/6to semestre: feb-jun 2025/algoritmos y análisis de datos
Subcarpeta encontrada: /content/drive/My Drive/6to semestre: feb-jun 2025/diagnostico financiero/reto
Subcarpeta encontrada: /content/drive/My Drive/6to semestre: feb-jun 2025/diagnostico financiero/m1
Subcarpeta encontrada: /content/drive/My Drive/6to semestre: feb-jun 2025/diagnostico financiero/m2
Subcarpeta encontrada: /content/drive/My Drive/6to semestre: feb-jun 2025/diagnostico financiero/m3
Contenido de /content/drive/My Drive/6to semestre: feb-jun 2025:
'algoritmos y análisis de datos' 'diagnostico financiero' 'series de tiempo'
Contenido de /content/drive/My Drive/6to semestre: feb-jun 2025/series de tiempo:
'A00838009_of COST WMT XLS ARIMA.html' HistoricalPrices-AAPL.xlsx
'A00838009_of COST WMT XLS ARIMA.ipynb' HistoricalPrices-MSFT.xlsx
act_2_2.ipynb 'notas series de tiempo.gdoc'
'ejemplo clase 21 feb 25 intro.ipynb'
In [11]:
from google.colab import drive
from google.colab import files
import os
# Montar Google Drive
drive.mount('/content/drive')
# Definir la ruta a la carpeta donde está el archivo (ajustada según la salida)
folder_path = '/content/drive/My Drive/6to semestre: feb-jun 2025/series de tiempo' # Ajusta según la salida
file_name = 'act clase bloomberg.ipynb' # Ajusta según la salida
full_path = os.path.join(folder_path, file_name)
# Verificar que el archivo exista
if os.path.exists(full_path):
print(f"El archivo {full_path} fue encontrado.")
else:
print(f"Error: El archivo {full_path} no fue encontrado. Verifica la ruta.")
raise FileNotFoundError("Archivo no encontrado.")
# Cambiar al directorio donde está el archivo
os.chdir(folder_path)
print(f"Directorio actual: {os.getcwd()}")
print("Archivos en el directorio:", os.listdir())
# Instalar nbconvert (por seguridad)
!pip install nbconvert
# Convertir el archivo a HTML (usar el nombre del archivo exacto)
!jupyter nbconvert --to html "{file_name}"
# Descargar el archivo HTML generado
html_file = file_name.replace('.ipynb', '.html') # Genera el nombre del archivo HTML
if os.path.exists(html_file):
files.download(html_file)
print(f"Archivo {html_file} descargado correctamente.")
else:
print(f"Error: No se generó el archivo {html_file}.")
raise FileNotFoundError("Archivo HTML no generado.")
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
Error: El archivo /content/drive/My Drive/6to semestre: feb-jun 2025/series de tiempo/act clase bloomberg (1).ipynb no fue encontrado. Verifica la ruta.
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) <ipython-input-11-c8a4fc2dc2c3> in <cell line: 0>() 16 else: 17 print(f"Error: El archivo {full_path} no fue encontrado. Verifica la ruta.") ---> 18 raise FileNotFoundError("Archivo no encontrado.") 19 20 # Cambiar al directorio donde está el archivo FileNotFoundError: Archivo no encontrado.